from typing import List
# 抄了讨论区的思路
class Solution:
    def threeSum(self, nums: List[int]) -> List[List[int]]:
        length = len(nums)
        if length < 3:
            return []

        result = set()
        nums.sort()
        prev = None # 加了个这就快了一大截
        for i, vi in enumerate(nums):
            if prev == vi:
                continue

            l = i + 1
            r = length - 1
            while l < r:
                vl, vr = nums[l], nums[r]
                s3 = vi + vl + vr
                if s3 == 0:
                    result.add((vi, vl, vr))
                    r -= 1
                    l += 1
                elif s3 > 0:
                    r -= 1
                else:
                    l += 1
            prev = vi
        return [list(t) for t in result]

solu = Solution()
print(solu.threeSum([-1, 0, 1, 2, -1, -4]))
print(solu.threeSum([0, 0, 0]))